var R = false, A = false, P = false;

$(function() {
	$("#addressId").val(-1);
	var ops = $("#address");
	
	$.ajax({
		type:"POST",
		url:"order.do?method=2",
		dataType:"json",
		async:false,
		success:function(data) {
			var addrs = data.addrs;
			appendOps(addrs);
			ops.change(function() {
				fillForm(addrs,$(this).val());
				});
		}
	});
	
	function appendOps(addrs) {
		for(var i = 0; i < addrs.length; i++) {
			ops.append("<option value='"+i+"'>"+i+":"+addrs[i].fullAddress.substr(0,6)+"...</option>");
		}
	}
	
	function fillForm(addrs,index) {
		if(index == -1) {
			$("#receiveName").val('');
			$("#fullAddress").val('');
			$("#postalCode").val('');
			$("#phone").val('');
			$("#mobile").val('');
			$("#addressId").val('');
			return;
		}
		A = true;
		R = true;
		P = true;
		
		$("#nameValidMsg>span").html('');
		$("#addressValidMsg>span").html('');
		$("#receiveName").val(addrs[index].receiveName);
		$("#fullAddress").val(addrs[index].fullAddress);
		$("#postalCode").val(addrs[index].postalCode);
		$("#phone").val(addrs[index].phone);
		$("#mobile").val(addrs[index].mobile);
		
		$("#addressId").val(addrs[index].id);
	}
	
});


//表单验证
$(function() {
	var X = false, D = false, M = false, Y = false,I = false;
	$("#btnClientRegister").click(function(){
		if(X && D && M && Y && I){
			return true;
		}else{
			return false;
		}
	});
	
	//收件人姓名验证
	$("#receiveName").blur(function() {
		X = false;
		var val = $(this).val();
		var info = $("#nameValidMsg");
		if (val == "") {
			info.text("姓名不能为空").css('color','red');
		}
		else {
			X = true;
			info.html("");
			info.html("<img src='./images/my/ajax_ok.png' />");
		}
	});
	
	//收货地址验证
	$("#fullAddress").blur(function() {
		D = false;
		var val = $(this).val();
		var info = $("#addressValidMsg");
		if (val == "") {
			info.html("");
			info.text("收件人不能为空").css('color','red');
		}
		else {
			D = true;
			info.html("");
			info.html("<img src='./images/my/ajax_ok.png' />");
		}
	});
	
	//手机号验证
	$("#mobile").blur(function() {
		M = false;
		var val = $(this).val();
		var info = $("#mobileValidMsg");
		if (val == "") {
			info.html("");
			info.text("手机号不能为空").css('color','red');
		}
		else if (!val.match(/^[0-9]{11}$/)) {
			info.html("");
			info.text("手机格式不正确");
		}
		else {
			M = true;
			info.html("");
			info.html("<img src='./images/my/ajax_ok.png' />");
		}
	});
	
	//电话验证
	$("#phone").blur(function() {
		I = false;
		var val = $(this).val();
		var info = $("#phoneValidMsg");
		if (val == "") {
			info.text("电话不能为空").css('color','red');
		}
		else {
			I = true;
			info.html("");
			info.html("<img src='./images/my/ajax_ok.png' />");
		}
	});
	
	//邮政编码 
	$("#postalCode").blur(function() {
		Y = false;
		var val = $(this).val();
		var info = $("#codeValidMsg");
		if (val == "") {
			info.html("");
			info.text("邮政编码不能为空").css('color','red');
		}
		else if (!val.match(/^[0-9]{6}$/)) {
			info.html("");
			info.text("邮政编码格式不正确");
		}
		else {
			Y = true;
			info.html("");
			info.html("<img src='./images/my/ajax_ok.png' />");
		}
	});
	
	
});